package cn.stylefeng.guns.modular.gridsystem.controller;

import cn.stylefeng.guns.core.annotion.BusinessLog;
import cn.stylefeng.guns.core.enums.LogAnnotionOpTypeEnum;
import cn.stylefeng.guns.core.pojo.response.ErrorResponseData;
import cn.stylefeng.guns.core.pojo.response.ResponseData;
import cn.stylefeng.guns.core.pojo.response.SuccessResponseData;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizGatewayScheduleParam;
import cn.stylefeng.guns.modular.gridsystem.service.BizGatewayScheduleService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * biz_gateway_schedule 控制器
 *
 * @author: ssy
 * @date: 2021/09/14 11:47
 */
@RestController
public class BizGatewayScheduleController {

    @Resource
    private BizGatewayScheduleService bizGatewayScheduleService;

    /**
     * 分页查询biz_gateway_schedule
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @GetMapping("/bizGatewaySchedule/page")
    @BusinessLog(title = "biz_gateway_schedule_分页查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData page(BizGatewayScheduleParam bizGatewayScheduleParam) {
        return new SuccessResponseData(bizGatewayScheduleService.pageList(bizGatewayScheduleParam));
    }

    /**
     * 获取全部biz_gateway_schedule
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @GetMapping("/bizGatewaySchedule/list")
    @BusinessLog(title = "biz_gateway_schedule_查询所有", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(BizGatewayScheduleParam bizGatewayScheduleParam) {
        return new SuccessResponseData(bizGatewayScheduleService.list(bizGatewayScheduleParam));
    }

    /**
     * 查看详情biz_gateway_schedule
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @GetMapping("/bizGatewaySchedule/detail")
    @BusinessLog(title = "biz_gateway_schedule_查看详情", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(BizGatewayScheduleParam.detail.class) BizGatewayScheduleParam bizGatewayScheduleParam) {
        return new SuccessResponseData(bizGatewayScheduleService.detail(bizGatewayScheduleParam));
    }

    /**
     * 添加biz_gateway_schedule
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @PostMapping("/bizGatewaySchedule/add")
    @BusinessLog(title = "biz_gateway_schedule_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(BizGatewayScheduleParam.add.class) BizGatewayScheduleParam bizGatewayScheduleParam) {
        Integer gateWayCount = bizGatewayScheduleService.getCountByGateWay(bizGatewayScheduleParam.getGatewayId());
        if (gateWayCount == 0){
//            String timeSchedule = bizGatewayScheduleParam.getTimeSchedule();
//            String[] split = timeSchedule.split("");
//            int count = 0;
//            for (String s : split) {
//                if("1".equals(s)){
//                    count++;
//                }
//            }
//            if(count>8){
//                return new ErrorResponseData(500,"最多添加8个时间段");
//            }
            bizGatewayScheduleService.add(bizGatewayScheduleParam);
            return new SuccessResponseData();
        }else {
            return new ErrorResponseData(500,"该网关已存在时间调度");
        }
    }

    /**
     * 删除biz_gateway_schedule
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @PostMapping("/bizGatewaySchedule/delete")
    @BusinessLog(title = "biz_gateway_schedule_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(BizGatewayScheduleParam.delete.class) BizGatewayScheduleParam bizGatewayScheduleParam) {
        bizGatewayScheduleService.delete(bizGatewayScheduleParam);
        return new SuccessResponseData();
    }

    /**
     * 编辑biz_gateway_schedule
     *
     * @author ssy
     * @date 2021/09/14 11:47
     */
    @PostMapping("/bizGatewaySchedule/edit")
    @BusinessLog(title = "biz_gateway_schedule_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(BizGatewayScheduleParam.edit.class) BizGatewayScheduleParam bizGatewayScheduleParam) {
//        String timeSchedule = bizGatewayScheduleParam.getTimeSchedule();
//        String[] split = timeSchedule.split("");
//        int count = 0;
//        for (String s : split) {
//            if("1".equals(s)){
//                count++;
//            }
//        }
//        if(count>8){
//            return new ErrorResponseData(500,"最多添加8个时间段");
//        }
        bizGatewayScheduleService.edit(bizGatewayScheduleParam);
        return new SuccessResponseData();
    }


}
